package com.linrui.basic.service.impl;

import com.linrui.basic.service.IFastDfsService;
import com.linrui.basic.utils.FastdfsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FastDfsServiceImpl implements IFastDfsService {

    @Value("${fastdfs.url}")
    private String fastDfsUrl;

    @Override
    public String upload(byte[] bytes, String extName) {
        return fastDfsUrl+ FastdfsUtil.upload(bytes,extName);
    }
    @Override
    public void delete(String path) {
        // path = http://115.159.217.249:8888/group1/M00/00/08/rBEAA2NjbN6AIGu-AADiwyyDRt439.jpeg
        // 调用fastdfsUtil.delete() 需要的参数是: groupName  group1  fileName  M00/00/08/rBEAA2NjbN6AIGu-AADiwyyDRt439.jpeg
        // 截取字符串,获取groupName fileName
        path = path.substring(fastDfsUrl.length()+1);       // group1/M00/00/08/rBEAA2NjbN6AIGu-AADiwyyDRt439.jpeg
        int indexOf = path.indexOf("/");    // 第一个/的位置
        String groupName = path.substring(0,indexOf);
        String fileName = path.substring(indexOf+1);
        // 删除文件
        FastdfsUtil.delete(groupName, fileName);
    }
}